<?php
/**
 * 插件帮手
 *
 * @author edikud
 * @package Helper
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Helper
{
	
    /**
     * 获取Widget_Metas_Category_List对象
     *
     * @access public
     * @return Widget_Metas_Category_List
     */
    public static function categories()
    {
        return Cola::widget('Widget_Metas_Category_List');
    }
	
    /**
     * 获取Widget_Styles对象
     *
     * @access public
     * @return Widget_Styles
     */
    public static function styles()
    {
        return Cola::widget('Widget_Styles');
    }
	
    /**
     * 获取Widget_Scripts对象
     *
     * @access public
     * @return Widget_Scripts
     */
    public static function scripts()
    {
        return Cola::widget('Widget_Scripts');
    }
	
	
    /**
     * 获取Widget_Options对象
     *
     * @access public
     * @return Widget_Options
     */
    public static function options()
    {
        return Cola::widget('Widget_Options');
    }

    /**
     * 获取Widget_Security对象
     *
     * @return Widget_Security
     */
    public static function security()
    {
        return Cola::widget('Widget_Security');
    }
	
    public static function element($tagName, $value = null, $attributes = null)
    {
		$element = new Element($tagName);
		null !== $attributes && $element->setAttr($attributes);

        return $element->set($value);
    }
	
    /**
     * 根据ID获取单个Widget对象
     *
     * @param string $table 表名, 支持 contents, comments, metas, users
     * @return Widget_$_Query
     */
    public static function widgetById($table, $pkId)
    {
        $table = ucfirst($table);
        if (!in_array($table, array('Contents', 'Comments', 'Metas', 'Users'))) {
            return NULL;
        }

        $keys = array(
            'Contents'  =>  'cid',
            'Comments'  =>  'coid',
            'Metas'     =>  'mid',
            'Users'     =>  'uid'
        );

        $className = "Widget_Abstract_{$table}";
        $key = $keys[$table];
        $widget = new $className(Request::instance(), Blank::instance());
        
        $widget->select()->where("{$key} = ?", $pkId)->limit(1)->
            fetchRow(array($widget, 'push'));

        return $widget;
    }

    /**
     * 导入语言项
     *
     * @access public
     * @param string $domain
     * @return void
     */
    public static function lang($domain)
    {
        $currentLang = I18n::getLang();
        if ($currentLang) {
            $currentLang = basename($currentLang);
            $fileName = dirname(__FILE__) . '/' . $domain . '/lang/' . $currentLang;
            if (file_exists($fileName)) {
                I18n::addLang($fileName);
            }
        }
    }

    /**
     * 增加路由
     *
     * @access public
     * @param string $name 路由名称
     * @param string $url 路由路径
     * @param string $widget 组件名称
     * @param string $do 组件动作
     * @param string $after 在某个路由后面
     * @return integer
     */
    public static function addRoute($name, $url, $widget, $params = NULL, $after = NULL)
    {
        $routingTable = self::options()->routingTable;
        if (isset($routingTable[0])) {
            unset($routingTable[0]);
        }

        $pos = 0;
        foreach ($routingTable as $key => $val) 
		{
            $pos ++;
            if ($key == $after) {
                break;
            }
        }

        $pre = array_slice($routingTable, 0, $pos);
        $next = array_slice($routingTable, $pos);

        $routingTable = array_merge($pre, array($name => array(
            'url'       =>  $url,
            'widget'    =>  $widget,
            'action'    =>  $action
        )), $next);
        self::options()->routingTable = $routingTable;

        return Cola::widget('Widget_Abstract_Options')->update('routingTable', serialize($routingTable));
    }

    /**
     * 移除路由
     *
     * @access public
     * @param string $name 路由名称
     * @return integer
     */
    public static function removeRoute($name)
    {
        $routingTable = self::options()->routingTable;
        if (isset($routingTable[0])) {
            unset($routingTable[0]);
        }

        unset($routingTable[$name]);
        self::options()->routingTable = $routingTable;

        return Cola::widget('Widget_Abstract_Options')->update('routingTable', serialize($routingTable));
    }

    /**
     * 增加action扩展
     *
     * @access public
     * @param string $actionName 需要扩展的action名称
     * @param string $widgetName 需要扩展的widget名称
     * @return integer
     */
    public static function addAction($actionName, $widgetName)
    {
        $actionTable = unserialize(self::options()->actionTable);
        $actionTable = empty($actionTable) ? array() : $actionTable;
        $actionTable[$actionName] = $widgetName;

        return Cola::widget('Widget_Abstract_Options')->update('actionTable', self::options()->actionTable = serialize($actionTable));
    }

    /**
     * 删除action扩展
     *
     * @access public
     * @param string $actionName
     * @return Cola
     */
    public static function removeAction($actionName)
    {
        $actionTable = unserialize(self::options()->actionTable);
        $actionTable = empty($actionTable) ? array() : $actionTable;

        if (isset($actionTable[$actionName])) {
            unset($actionTable[$actionName]);
            reset($actionTable);
        }

        return Cola::widget('Widget_Abstract_Options')->update('actionTable', self::options()->actionTable = serialize($actionTable));
    }

    /**
     * 增加一个菜单
     *
     * @access public
     * @param string $menuName 菜单名
     * @return integer
     */
    public static function addMenu($menuName)
    {
	    $panelTable = unserialize(self::options()->panelTable);
        $panelTable['parent'] = empty($panelTable['parent']) ? array() : $panelTable['parent'];
        $panelTable['parent'][] = $menuName;

        Cola::widget('Widget_Abstract_Options')->update('panelTable', self::options()->panelTable = serialize($panelTable));

        end($panelTable['parent']);
        return key($panelTable['parent']) + 10;
    }

    /**
     * 移除一个菜单
     *
     * @access public
     * @param string $menuName 菜单名
     * @return integer
     */
    public static function removeMenu($menuName)
    {
        $panelTable = unserialize(self::options()->panelTable);
        $panelTable['parent'] = empty($panelTable['parent']) ? array() : $panelTable['parent'];

        if (false !== ($index = array_search($menuName, $panelTable['parent']))) {
            unset($panelTable['parent'][$index]);
        }

        Cola::widget('Widget_Abstract_Options')->update('panelTable', self::options()->panelTable = serialize($panelTable));

        return $index + 10;
    }

    /**
     * 增加一个面板
     *
     * @access public
     * @param integer $index 菜单索引
     * @param string $title 面板标题
     * @param string $subTitle 面板副标题
     * @param string $url 项目链接
     * @param string $access 进入权限
     * @param boolean $hidden 是否隐藏
     * @param string $addLink 新增项目链接, 会显示在页面标题之后
     * @return integer
     */
    public static function addPanel($index, $title, $subTitle, $url, $access, $hidden = false, $addLink = '')
    {
        $panelTable = unserialize(self::options()->panelTable);
        $panelTable['child'] = empty($panelTable['child']) ? array() : $panelTable['child'];
        $panelTable['child'][$index] = empty($panelTable['child'][$index]) ? array() : $panelTable['child'][$index];
        $panelTable['child'][$index][] = array($title, $subTitle, $url, $access, $hidden, $addLink);

        Cola::widget('Widget_Abstract_Options')->update('panelTable', self::options()->panelTable = serialize($panelTable));

        end($panelTable['child'][$index]);
        return key($panelTable['child'][$index]);
    }

    /**
     * 移除一个面板
     *
     * @access public
     * @param integer $index 菜单索引
     * @param string $fileName 文件名称
     * @return integer
     */
    public static function removePanel($index, $url)
    {
        $panelTable = unserialize(self::options()->panelTable);
        $panelTable['child'] = empty($panelTable['child']) ? array() : $panelTable['child'];
        $panelTable['child'][$index] = empty($panelTable['child'][$index]) ? array() : $panelTable['child'][$index];

        $return = 0;
        foreach ($panelTable['child'][$index] as $key => $val) {
            if ($val[2] == $url) {
                unset($panelTable['child'][$index][$key]);
                $return = $key;
            }
        }

        Cola::widget('Widget_Abstract_Options')->update('panelTable', self::options()->panelTable = serialize($panelTable));
		
        return $return;
    }
    
    /**
     * 手动配置插件变量
     * 
     * @access public
     * @static
     * @param mixed $pluginName 插件名称
     * @param array $settings 变量键值对
     * @param bool $isPersonal. (default: false) 是否为私人变量
     * @return void
     */
    public static function configPlugin($pluginName, array $settings, $isPersonal = false)
    {
        if (empty($settings)) {
            return;
        }
        
        Widget_Plugins_Edit::configPlugin($pluginName, $settings, $isPersonal);
    }
}
